package sql;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Qus {
        public static List<Cont> getData(File src) throws IOException {
            List<Cont> list = new ArrayList<Cont>();
            BufferedReader br =new BufferedReader(new FileReader(src));
            int flag=1;
            String str;
            Cont cont = null;
            while(null!=(str=br.readLine())){
                if(flag%4==1){
                    cont= new Cont();
                    cont.setQus_content(str);
                }
                if (flag%4==2){
                    cont.setQus_a(str);
                }
                if(flag%4==3){
                    cont.setQus_b(str);
                    list.add(cont);
                }
                flag++;
            }
            br.close();
            return list;
        }
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
            File file = new File("d:\\question.txt");
            List<Cont> list = null;
            try {
                list=getData(file);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("文件读取失败");
            }
            System.out.println("遍历-----------------------");
            for (Cont q:list
            ) {
                System.out.println(q.getQus_content());
            }
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection
                    ("jdbc:mysql://localhost:3306/questions?serverTimezone=UTC","root","12345");
            PreparedStatement psql;
            psql=connection.prepareStatement("INSERT INTO biz_question (qus_id,qus_content,qus_a,qus_b)"+" VALUES (?,?,?,?)");
            for (int i = 0; i <list.size() ; i++) {
                psql.setInt(1,i+1);
                psql.setString(2,list.get(i).getQus_content());
                psql.setString(3,list.get(i).getQus_a());
                psql.setString(4,list.get(i).getQus_b());
                psql.executeUpdate();

            }

        }
    }

